﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyCommerce.Presentation.UsersAndSecurity.IViews;
using Innova.FoundationFramework.DataTypes;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.Business.UsersAndSecurity;
using Innova.Microfinanzas.BusinessEntities.SystemEnums;

namespace MyCommerce.Presentation.UsersAndSecurity.Presenters
{
    public class UserFillPresenter
    {
        private IUserFillView pViewToPresent;

        public UserFillPresenter(IUserFillView vViewToPresent)
        {
            pViewToPresent = vViewToPresent;
            pViewToPresent.OnCancel += new EventHandler(pViewToPresent_OnCancel);
            pViewToPresent.OnLoadView += pViewToPresent_OnLoadView;
            pViewToPresent.OnSave += new EventHandler(pViewToPresent_OnSave);
        }

        private void pViewToPresent_OnSave(object sender, EventArgs e)
        {
            try
            {
                UserManager userMgr = new UserManager();
                User objUserToSave = pViewToPresent.Get();
                if (pViewToPresent.OptionViewSelect == OptionSelect.Create)
                    userMgr.CreateUser(objUserToSave);
                else
                {
                    objUserToSave.IdUser = pViewToPresent.IdItemEdit;
                    userMgr.UpdateUser(objUserToSave);
                }
                pViewToPresent.CloseAndSetResult(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void pViewToPresent_OnLoadView(object sender, GenericEventArgs<User> e)
        {
            EmployeeManager employeeMgr = new EmployeeManager();
            RoleManager roleMgr = new RoleManager();
            pViewToPresent.ColEmployeesLoad = employeeMgr.GetEmployeeActives();
            pViewToPresent.ColRolesLoad = roleMgr.GetRoleActives();

            if (e.EventData != null)
                pViewToPresent.Set(e.EventData);
        }

        private void pViewToPresent_OnCancel(object sender, EventArgs e)
        {
            pViewToPresent.CloseAndSetResult(false);
        }
    }
}
